package com.aaa.project.controller;


import com.aaa.entity.UComment;
import com.aaa.entity.UUserInfo;
import com.aaa.project.service.IGGoodsService;
import com.aaa.project.service.IUCommentService;
import com.aaa.project.service.IUUserInfoService;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangxuhui
 * @since 2023-02-08
 */
@RestController
@RequestMapping("/u-comment")
public class UCommentController {
        @Resource
    private IUCommentService iuCommentService;
        @Resource
        private IGGoodsService goodsService;
        @Resource
        private IUUserInfoService userInfoService;

    /**
     * 查找我的评论
     * @param principal
     * @param pageSize
     * @param pageCurrent
     * @return
     */
        @GetMapping("findMyComment/{id}/{pageSize}/{pageCurrent}")
    public Result findMyComment(Principal principal,@PathVariable Integer pageSize,@PathVariable Integer pageCurrent,@PathVariable  String id){
            return new Result(iuCommentService.findMyComment(principal,pageSize,pageCurrent,id));
        }


        @GetMapping("findAllComment/{id}/{pageSize}/{pageCurrent}")
    public Result findAllComment(@PathVariable Integer pageSize,@PathVariable Integer pageCurrent,@PathVariable String id){
            return new Result(iuCommentService.findAllComment(pageCurrent,pageSize,id));
        }

    /**
     * 查询商品
     * @param id
     * @return
     */
    @PostMapping("findGoodsByGoodsid/{id}")
    public Result findGoodsByGoodsid(@PathVariable String id){
        List<Map<String, Object>> all = goodsService.findAll(id);
        return new Result<>(all);
    }

    /**
     * 关注店铺
     * @param bid
     * @param principal
     * @return
     */
    @PostMapping("isCollectionBusiness")
    public Result isCollectionBusiness(@RequestParam String bid,Principal principal ){
        return new Result<>(iuCommentService.findGuan(bid,principal));
    }

    /**
     * 删除评论
     * @param id 商品id
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        QueryWrapper wrapper=new QueryWrapper<>();
        System.out.println("id = " + id);
        wrapper.eq("id",id);
        UComment comment=iuCommentService.getOne(wrapper);
        comment.setIsDeleted(1);
        return new Result<>(iuCommentService.saveOrUpdate(comment));
    }

    /**
     * 添加评论
     * @param comment
     * @param principal
     * @return
     */
    @PostMapping("addComment")
    public Result addComment(@RequestBody UComment comment,Principal principal){
        String name = principal.getName();
        // 根据当前的人的名字用户的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=userInfoService.getOne(queryWrapper);
        comment.setUserid(uUserInfo.getId());
        comment.setCreatetime(LocalDateTime.now());
        comment.setIsDeleted(0);
        String img=String.join(",",comment.getImgs());
        comment.setImg(img);
        return new Result<>(iuCommentService.save(comment));
    }
    /**
     * 编辑评论
     */
    @PostMapping("updateComment")
    public Result updateComment(UComment uComment,String img){
        System.out.println("uComment = " + uComment);
        uComment.setImg(img);
        uComment.setUpdatetime(LocalDateTime.now());
        boolean b = iuCommentService.updateById(uComment);
        return new Result(b);
    }


}

